package com.aos.erp.gov.ex.service.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.gov.ex.enums.TicketStatus;
import com.aos.erp.gov.ex.mapper.GovSchoolTicketMapper;
import com.aos.erp.gov.ex.model.GovSchoolTicket;
import com.aos.erp.gov.ex.model.dto.GatherSchoolDTO;
import com.aos.erp.gov.ex.service.GovExSyncTaskService;
import com.aos.erp.gov.ex.service.GovSchoolTicketService;
import com.aos.erp.gov.ex.service.remote.SchoolService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class GovSchoolTicketServiceImpl extends ServiceImpl<GovSchoolTicketMapper, GovSchoolTicket> implements GovSchoolTicketService {

    @Resource
    private GovExSyncTaskService taskService;

    @Resource
    private SchoolService schoolService;

    @Override
    public GovSchoolTicket getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void addBean(GovSchoolTicket bean) {
        bean.setCreateTime(Calendar.getInstance().getTime());
        bean.setUpdateTime(bean.getCreateTime());
        bean.setRefreshTime(bean.getCreateTime());
        bean.setStatus(TicketStatus.work);
        baseMapper.insert(bean);
    }

    @Override
    public PageBean<GovSchoolTicket> queryByPage(long pageIndex, long pageSize) {

        IPageBean<GovSchoolTicket> pageBean = baseMapper.createBizPage(pageIndex, pageSize);
        Condition condition = new Condition(GovSchoolTicket.class);
        condition.orderByDesc("id");

        return (PageBean<GovSchoolTicket>) baseMapper.selectPage(pageBean, condition.getWrapper());
    }

    @Override
    public void saveCookies(String webCode, String account, String deviceId, String cookie) {
        QueryWrapper<GovSchoolTicket> queryWrapper = baseMapper.createQuery();
        queryWrapper.eq("account", account);//根据122的设备，account来获取
        queryWrapper.or().eq("device_id", deviceId);

        GovSchoolTicket bean = baseMapper.selectOneBean(queryWrapper);

        if (bean == null) {
            bean = new GovSchoolTicket();
            List<GatherSchoolDTO> list = schoolService.getSchoolByDev(deviceId, account);

            if (list != null && list.size() > 0) {
                bean.setSchoolId(list.get(0).getId());
                bean.setSchoolName(list.get(0).getName());
            }

            bean.setAccount(account);
            bean.setDeviceId(deviceId);
            bean.setCookies(cookie);
            bean.setWebCode(webCode);
            this.addBean(bean);

            taskService.saveNewAccount(bean.getId(), bean.getAccount());
        } else {
            this.updateCookies(bean.getId(), cookie);
        }
    }

    @Override
    public void updateCookies(long id, String cookies) {
        baseMapper.updateCookies(id, cookies, TicketStatus.work, Calendar.getInstance().getTime());
    }

    @Override
    public void updateSuccessStatus(long id, Date refreshTime) {
        baseMapper.updateRefreshFlag(id, refreshTime, TicketStatus.work);
    }

    @Override
    public void updateTimeOutStatus(long id, Date refreshTime) {
        baseMapper.updateRefreshFlag(id, refreshTime, TicketStatus.timeout);
    }


    @Override
    public List<GovSchoolTicket> getTopNeedRefresh(Date minTime) {
        Condition condition = new Condition(GovSchoolTicket.class);

        condition.eqFiled("status", TicketStatus.work);    //查询出正常的
        condition.le("refreshTime", minTime);//小于某个时间点的

        condition.orderByAsc("refreshTime");//越久的越先刷新

        IPage page = baseMapper.createPlusPage(1, 10);
        ((Page) page).setOptimizeCountSql(false);//不count查询

        baseMapper.selectPage(page, condition.getWrapper());
        return page.getRecords();
    }

}